package lms.dao.impl;

import lms.dao.OperationDao;
import lms.entity.Book;
import lms.utils.BookRowMapper;
import lms.utils.DBUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 图书增删改查
 * @author: Grape_Pip
 * @time: 2021/06/22
 */
public class BookDaoImpl implements OperationDao<Book> {
    /**
     * 获取JDBCTemplate对象
     **/
    private JdbcTemplate template = new JdbcTemplate(DBUtils.getDataSource());
    /**
     * 数据库连接对象
     */
    Connection conn = null;
    /**
     * 数据库操作对象
     */
    PreparedStatement pstmt = null;
    /**
     * 结果集
     */
    ResultSet rs = null;


    @Override
    public List<Book> selectAllInfo() {
        List<Book> bookList ;
        try {
            String sql = "select * from l_book ";
            // 返回 bookList
            bookList = template.query(sql, new BookRowMapper());
            return bookList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int deleteById(int bid) {

        try {


            String sql = "delete from l_book " +
                    "WHERE bISBN=?";
            int res = template.update(sql, bid);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int updateById(int bid, Book bookinfo) {
        try {

            String sql = "UPDATE l_book set bISBN=?,bname=?,bauthor=?,bstocks=?, bpublish=?,bpublishtime=?,btype=? " +
                    "WHERE bISBN=?; ";
            int res = template.update(sql, bookinfo.getBookISBN(), bookinfo.getBookName(), bookinfo.getBookAuthor(), bookinfo.getBookStock(), bookinfo.getBookpublish(), bookinfo.getBookPublishDate(), bookinfo.getBookType(), bid);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int updateStockById(int bid, int bNum) {
        try {

            String sql = "UPDATE l_book set bstocks=? " +
                    "WHERE bISBN=?; ";
            int res = template.update(sql, bNum,bid);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    @Override
    public int insertInfo(Book book) {
        try {

            String sql = "insert into l_book (bISBN,bname,bauthor,bstocks,bpublish,bpublishtime,btype) " +
                    "VALUES (?,?,?,?,?,?,?)";
            int res = template.update(sql, book.getBookISBN(), book.getBookName(), book.getBookAuthor(), book.getBookStock(), book.getBookpublish(), book.getBookPublishDate(), book.getBookType());
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Book selectById(int bid) {
        try {
            String sql = "select * from l_book where bISBN = ?";
            // 返回 Student对象
            return template.queryForObject(sql, new BookRowMapper(), bid);
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }
    }
}
